﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace FHJFPro.Utils.Helper
{
    public static class HttpHelper
    {
        public static string Post(string url, object data, string contentType = "application/json;charset=UTF-8", Dictionary<string, string> headers = null)
        {
            var json = string.Empty;
            if (contentType == "application/json;charset=UTF-8")
            {
                json = data.ToJson();
            }
            else
            {
                json = (string)data;
            }
            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = contentType;
            if (headers != null && headers.Count != 0)
            {
                foreach (var item in headers)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            byte[] payload;
            payload = System.Text.Encoding.UTF8.GetBytes(json);
            request.ContentLength = payload.Length;
            string strValue = "";

            Stream writer = request.GetRequestStream();
            writer.Write(payload, 0, payload.Length);
            writer.Close();
            System.Net.HttpWebResponse response;
            response = (System.Net.HttpWebResponse)request.GetResponse();

            using (Stream s = response.GetResponseStream())
            {
                string StrDate = "";
                using (StreamReader reader = new StreamReader(s, Encoding.UTF8))
                {
                    while ((StrDate = reader.ReadLine()) != null)
                    {
                        strValue += StrDate;
                    }
                }
            }

            return strValue;
        }

        public static T Post<T>(string url, object data, string contentType = "application/json;charset=UTF-8", Dictionary<string, string> headers = null)
             where T : class, new()
        {
            var json = data.ToJson();
            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = contentType;
            if (headers != null && headers.Count != 0)
            {
                foreach (var item in headers)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            byte[] payload;
            payload = System.Text.Encoding.UTF8.GetBytes(json);
            request.ContentLength = payload.Length;
            string strValue = "";

            Stream writer = request.GetRequestStream();
            writer.Write(payload, 0, payload.Length);
            writer.Close();
            System.Net.HttpWebResponse response;
            response = (System.Net.HttpWebResponse)request.GetResponse();

            using (Stream s = response.GetResponseStream())
            {
                string StrDate = "";
                using (StreamReader reader = new StreamReader(s, Encoding.UTF8))
                {
                    strValue = reader.ReadToEnd();
                    //一下一行一行地读取速度太慢了
                    //while ((StrDate = reader.ReadLine()) != null)
                    //{
                    //    strValue += StrDate;
                    //}
                }
            }
            if (string.IsNullOrEmpty(strValue))
            {
                strValue = "{}";
            }
            return strValue.FromJson<T>();
        }

        public static string Get(string url)
        {
            string result = string.Empty;

            HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
            wbRequest.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)wbRequest.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader sReader = new StreamReader(responseStream))
                {
                    result = sReader.ReadToEnd();
                }
            }

            return result;
        }

        public static T Get<T>(string url)
           where T : class, new()
        {
            HttpClient httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = httpClient.GetAsync(url).Result;

            string result = string.Empty;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
            }

            return result.FromJson<T>();
        }
        public static long GetFileSize(string url)
        {
            HttpWebResponse response = null;
            long contentLength = -1L;
            try
            {
                response = (HttpWebResponse)GetRequest(url).GetResponse();
                contentLength = response.ContentLength;
                if (contentLength == -1L)
                {
                    return contentLength;
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            return contentLength;
        }

        private static HttpWebRequest GetRequest(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Credentials = CredentialCache.DefaultCredentials;
            return request;
        }
    }
}
